package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关功能")
@RequiredArgsConstructor
public class DishController {

    private final DishService dishService;

    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result add(@RequestBody DishDTO dto){
        log.info("接收到的参数为:{}",dto);
        dishService.add(dto);
        return Result.success();
    }

    /**
     * 菜品分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result pageQuery(DishPageQueryDTO dto){
        log.info("接收到的参数为:{}",dto);
        PageResult pageResult = dishService.pageQuery(dto);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除菜品
     */
    @DeleteMapping
    @ApiOperation("根据id删除菜品")
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("接收到的参数为:{}",ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("/{dishId}")
    @ApiOperation("根据id查询菜品")
    public Result queryById(@PathVariable Long dishId){
        return Result.success(dishService.queryById(dishId));
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public Result update(@RequestBody DishDTO dto){
        dishService.update(dto);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result queryAllByCategoryId(Integer categoryId){
        List<Dish> dishes = dishService.queryAllByCategoryId(categoryId);
        return Result.success(dishes);
    }
}
